﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TemplateWizard;
using EnvDTE;

namespace MVC4WPF.Automation.Wizards
{
    /// <summary>
    /// Base class for all MVC4WPF Wizards. Implements the IWizard interface
    /// </summary>
    public abstract class MVC4WPFWizardBase : IWizard
    {
        //All Public properties go in this region
        #region Properties

        /// <summary>
        /// Holds a reference to the current project the wizard will be executing over.
        /// </summary>
        public Project CurrentProject { get; set; }

        /// <summary>
        /// Holds a flag for whether or not the project item should be added.
        /// </summary>
        /// <remarks>
        /// Initialized to true in the base constructor.
        /// </remarks>
        public Boolean ShouldAddItem { get; set; }

        #endregion

        #region Constructor

        /// <summary>
        /// Public constructor for the MVC4WPFWizardBase class.
        /// </summary>
        public MVC4WPFWizardBase()
        {
            //set the value to true by default
            this.ShouldAddItem = true;
        }

        #endregion

        //All Public and Private methods for class functionality go here
        #region Class Logic

        public abstract void ExecuteWizard(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams);

        public String FormatDictionaryParameterName(String parameterName)
        {
            return String.Format("{0}{1}{0}", "$", parameterName);
        }

        #endregion

        //All interfaces are implemented below
        #region IWizard Members

        /// <summary>
        /// Empty overridable method.
        /// </summary>
        public virtual void BeforeOpeningFile(ProjectItem projectItem)
        {
            return;
        }

        /// <summary>
        /// Empty overridable method.
        /// </summary>
        public virtual void ProjectFinishedGenerating(Project project)
        {
            return;
        }

        /// <summary>
        /// Empty overridable method.
        /// </summary>
        public virtual void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
            return;
        }

        /// <summary>
        /// Empty overridable method.
        /// </summary>
        public virtual void RunFinished()
        {
            return;
        }

        /// <summary>
        /// Executed before the template is executed. Calls this.ExecuteWizard.
        /// </summary>
        /// <remarks>
        /// This also sets up the CurrentProject property of the base class based on the _DTE object coming in through the automationObject parameter.
        /// </remarks>
        /// <param name="automationObject">The _DTE com object.</param>
        /// <param name="replacementsDictionary">A key value collection of replacement pairs for the template.</param>
        /// <param name="runKind">The kind of wizard run.</param>
        /// <param name="customParams">Any custom parameters provided by the template not already included in the replacementsDictionary</param>
        public virtual void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            _DTE visualStudioEnvironment = automationObject as _DTE;

            if (visualStudioEnvironment != null)
            {
                Object[] solutionProjects = visualStudioEnvironment.ActiveSolutionProjects as Object[];

                if (solutionProjects != null)
                {
                    this.CurrentProject = solutionProjects[0] as Project;
                }
            }

            this.ExecuteWizard(automationObject, replacementsDictionary, runKind, customParams);
        }

        /// <summary>
        /// This method returns the vlaue held in the ShouldAddItem property.
        /// </summary>
        public virtual bool ShouldAddProjectItem(string filePath)
        {
            return this.ShouldAddItem;
        }

        #endregion
    }
}
